<?php
namespace App\Http\Middleware;
use Closure;
use App\Models\V1\TokenModel;
use App\Common\ApiData;



class CheckToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token          = $request->input('token');
        $tokenModel     = new TokenModel();
        $apiData        = new ApiData();


        //检查token的有效性
        if(empty($token)){
            $apiData->message   = "非法,缺少token";
            $apiData->code      = "402";
            return $apiData->send();
        }

        $result = $tokenModel->check_token($token);
        if($result === false){
            $apiData->message   = "token无效";
            $apiData->code      = "403";
            return $apiData->send();
        }
        return  $next($request);
    }
}